//
//  WelcomeViewController.swift
//  Weibo
//
//  Created by 冯童童 on 16/1/17.
//  Copyright © 2016年 冯童童. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {

    private lazy var  backImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var  headImageView : UIImageView = UIImageView()
    
    private lazy var welcomeLabel : UILabel = UILabel(title: (UserAccountViewModel().userName ?? "") + " 欢迎归来", fontSize: 18, color: UIColor.darkGrayColor())
    
    
//    {
//        
//       let l = UILabel()
//       l.text = (UserAccountViewModel().userName ?? "") + " 欢迎归来"
//       l.textColor = UIColor.darkGrayColor()
//        l.font = UIFont.systemFontOfSize(18)
//        
//        l.sizeToFit()
//        return l
//    }()

    override func loadView() {
    view = backImageView
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

       setupUI()
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        showAnimation()

    }
    

    private func setupUI() {
        self.view.addSubview(headImageView)
        self.view.addSubview(welcomeLabel)
        headImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.centerX.equalTo(self.view.snp_centerX)
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(self.view.snp_centerX)

        }
        
        headImageView.layer.cornerRadius = 45
        headImageView.layer.masksToBounds = true
        welcomeLabel.alpha = 0
        headImageView.sd_setImageWithURL(UserAccountViewModel().headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    
    private func showAnimation() {
        
        
        let offset = -UIScreen.mainScreen().bounds.height + 160
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options:[], animations: { () -> Void in
            
            self.headImageView.snp_updateConstraints { (make) -> Void in
                
                make.bottom.equalTo(self.view.snp_bottom).offset(offset)
                
            }
            
             self.view.layoutIfNeeded()
            
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        
                        print("OK")
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: nil)
     
                })
        }
        
        
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
